﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Communities.aspx.cs" Inherits="admin_Pages_Communities" MasterPageFile="~/admin/admin1.master"%>
<%@ Import Namespace="CoyoEden" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphAdmin" Runat="Server">
	<div id="toolsWrapper" style="display:none;">
		<input type="hidden" id="txtUser" value="<%=Page.User.Identity.Name %>" name="user"/>
		<span id="ddlCParentWrapper0">
			<asp:DropDownList ID="ddlNewParent" width="200" runat="server"/>
		</span>
		<div id="editBox"></div>
	</div>
  <table id="flexigridCommunity" style="display:none;"></table>
  <script type="text/javascript">

  	$("#flexigridCommunity").flexigrid
		(
  	{
  		url: '../../App_Services/Community.asmx/GetCommunities',
  		urlType: "asmx",
  		dataType: 'json',
  		contentType: "application/json;charset=utf-8",
  		colModel: [
  		{ display: '标题', name: 'name', width: 120, sortable: true, align: 'center' },
  		{ display: '描述', name: 'description', width: 180, sortable: false, align: 'left' },
  		{ display: '简称', name: 'slug', width: 120, sortable: false, align: 'left' },
  		{ display: '父社区', name: 'parent', width: 120, sortable: true, align: 'left' }
				],
  		buttons: [
  		{ name: 'Add', display: '添加', bclass: 'add', onpress: function(_params) {
  			Community.PreEdit(-1, _params);
  		}
  		},
  		{ name: 'Delete', display: '删除', bclass: 'delete', onpress: function(_params) {
  			Community.Del(_params);
  		}
  		},
  		{ separator: true }
				],
  		searchitems: [
  		{ display: 'Name', name: 'Name' },
  		{ display: 'Description', name: 'Description', isdefault: true }
				],
  		sortname: "name",
  		sortorder: "asc",
  		usepager: true,
  		title: 'Communities',
  		useRp: true,
  		rp: 20,
  		showTableToggleBtn: false,
  		width: 700,
  		height: 200,
  		preProcess: function(data) {
  			return data.d;
  		},
  		onRowDblClick: function(_params) {
  			Community.PreEdit(_params.id, _params);
  		}
  	}
		);
  	var Community = function() {
  		var _ajaxDefaultOpts = {
  			type: 'post',
  			dataType: 'json',
  			contentType: 'application/json;charset=utf-8'
  		};
  		return {
  			UploadOkCallback: function(imgsrc) {
  				$("#imgNCIcon").attr("src", imgsrc);
  				$("#txtNCIcon").val(imgsrc);
  			}, //uploadok callback
  			DefaultDialogOpts: {
  				modal: true,
  				width: 600,
  				height: 400,
  				resizable: false,
  				overlay: { opacity: 0.5, background: 'black' }
  			},
  			Asmx: '<%=Utils.AbsoluteWebRoot.ToString()+"App_Services/Community.asmx/" %>',
  			PreEdit: function(id, opts) {
  				if (id == -1 && $("#editBox").html() != "") {
  					$("#editBox").show().dialog(Community.DefaultDialogOpts);
  					//focus to first input
  					$("#txtCTitle").select();
  					return;
  				};
  				if (id == -1) {
  					Community.DefaultDialogOpts.close = function() {
  						$("#toolsWrapper").append($("#editBox").dialog("destroy").remove());
  					};
  				} else {
  					Community.DefaultDialogOpts.close = function() {
  						$("#toolsWrapper").append($("#editBox").dialog("destroy").empty().remove());
  					};
  				}

  				//prepare ajax opts
  				var ajaxOpts = $.extend({
  					url: Community.Asmx + "PreEdit",
  					data: $.toJSON({ Params: { id: id, user: $("#txtUser").val()} }),
  					success: function(data) {
  						$("#editBox").html(data.d.Body).show().dialog(Community.DefaultDialogOpts);
  						//Fetch Category dropdown list
  						var communities = $("#ddlCParentWrapper0").children().clone().attr("id", "ddlCParent").attr("name", "CommunityParent");
  						$("#ddlCParentWrapper").append(communities);
  						//focus to first input
  						$("#txtCTitle").select();
  					} //endof success
  				}, _ajaxDefaultOpts);

  				//Request to server
  				$.ajax(ajaxOpts);
  				ajaxOpts = null;
  			}, //endof preEdit
  			Del: function(opts) {
  				var keys = $.map($.makeArray(opts.rows), function(node, i) {
  					return node.id.substring(3); //remove id prefix 'row'
  				});
  				if (keys.length == 0) {
  					alert("没有选中任何行");
  					return;
  				}
  				if (!window.confirm("确认删除选定的" + keys.length + "行记录么?")) {
  					return;
  				};
  				//Request to server
  				var ajaxOpts = $.extend({
  					url: Community.Asmx + "Delete",
  					data: $.toJSON({ Params: { keys: keys.join('|'), user: $("#txtUser").val()} }),
  					success: function(data) {
  						$("#flexigridCommunity").flexReload();
  					}
  				}, _ajaxDefaultOpts);
  				$.ajax(ajaxOpts);
  				ajaxOpts = null;
  			}, //endof delete
  			Save: function(opts) {
  				//get all inputs for category edit
  				var data = $(":input").filter("[name^='Community'],[name='user']").serializeArray();
  				//requset to server
  				var ajaxopts = $.extend({ url: Community.Asmx + "Update",
  					data: $.toJSON({ Params: data }),
  					success: function(msg) {
  						msg = msg.d;
  						if (msg.IsOk) {
  							$("#statusbar").html("保存成功").css({ color: 'blue' }).show().fadeOut(2000, function() {
  								//focus to first input
  								$("#txtCTitle").select();
  							}); //.fadeOut(3000, Category.DefaultDialogOpts.close);
  							$("#flexigridCommunity").flexReload();
  						} else {
  							$("#statusbar").html(msg.Body).css({ color: 'red' }).fadeOut(3000);
								//Todo:highlight error elements
  						}
  					}
  				}, _ajaxDefaultOpts);
  				$.ajax(ajaxopts);
  				ajaxopts = null;
  			} //Endof Save
  		}
  	} ();
	</script>
</asp:Content>
